package com.seafwg.store.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author seafwg
 * @Date 2022/3/27 12:07 上午
 * @Version 1.0
 * @description 登录拦截器的功能：通过请求响应获取session对象，从session中获取uid，
 *              如果存在放行，不存在重定向到登录页面
 */

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler
    ) throws Exception {
        Object uid = request.getSession().getAttribute("uid");
        if (uid == null) {
            response.sendRedirect("/store/web/login.html");
            return false;
        }
        return true;
    }
}
